package com.flycms.module.comment.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.comment.dao.AnswersCommentDao;
import com.flycms.module.comment.dao.AnswersCountDao;
import com.flycms.module.comment.dao.AppCommentDao;
import com.flycms.module.comment.dao.AppCountDao;
import com.flycms.module.comment.dao.ArticlesCommentDao;
import com.flycms.module.comment.dao.ArticlesCountDao;
import com.flycms.module.comment.model.AnswersOptionCount;
import com.flycms.module.comment.model.Comments;
import com.flycms.module.comment.model.FlyContentType;
import com.flycms.module.comment.model.OptionCount;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.model.Answers;

@Service
public class CommentsService {

    @Autowired
    protected AnswersCommentDao answersCommentDao;

    @Autowired
    protected AnswersCountDao answersCountDao;

    @Autowired
    protected AnswersDao answerDao;

    @Autowired
    protected ArticlesCommentDao articlesCommentDao;

    @Autowired
    protected ArticlesCountDao articlesCountDao;

    @Autowired
    protected AppCommentDao appCommentDao;

    @Autowired
    protected AppCountDao appCountDao;

    @Transactional
    public OptionCount addComment(Comments comment, FlyContentType type) throws Exception {
        int i = 0;
        switch (type) {
          case ANSWER:
              i = answersCommentDao.addComment(comment);
              break;
          case ARTICLE:
              i = articlesCommentDao.addComment(comment);
              break;
          case INDUSTRY_APP:
              i = appCommentDao.addComment(comment);
              break;
          default:
              break;
        }
        OptionCount o = null;
        if (i != 0) {
            o = recordCommentCount(comment, type);
        }
        return o;
    }

    public OptionCount recordCommentCount(Comments comment, FlyContentType type) {
        OptionCount c = null;
        int i = 0;
        switch (type) {
          case QUESTION:
//              o = questionsVoteDao.queryVoteCount(vote.getContentId());
//              c = questionsCountDao.queryCountById(vote.getContentId());
//              if (c == null) {
//                  o.setId(vote.getContentId());
//                  o.setCollectCount(0);
//                  o.setCommentCount(0);
//                  questionsCountDao.addCount(o);
//              } else {
//                  c.setUpCount(o.getUpCount());
//                  c.setDownCount(o.getDownCount());
//                  questionsCountDao.updateCount(c);
//              }
              break;
          case ANSWER:
              i = answersCommentDao.queryCommentCount(comment.getContentId());
              AnswersOptionCount aoc = answersCountDao.queryCountById(comment.getContentId());
              Answers a = answerDao.queryAnswerById(comment.getContentId(), null);
              if (aoc == null) {
                  aoc = new AnswersOptionCount();
                  aoc.setUpCount(0);
                  aoc.setDownCount(0);
                  aoc.setId(a.getId());
                  aoc.setQuestionId(a.getQuestionId());
                  aoc.setCommentCount(i);
                  aoc.setCollectCount(0);
                  answersCountDao.addCount(aoc);
              } else {
                  aoc.setCommentCount(i);
                  answersCountDao.updateCount(aoc);
              }
              break;
          case ARTICLE:
              i = articlesCommentDao.queryCommentCount(comment.getContentId());
              c = articlesCountDao.queryCountById(comment.getContentId());
              if (c == null) {
                  c = new OptionCount(comment.getContentId(), 0, 0, 0, i, 0);
                  articlesCountDao.addCount(c);
              } else {
                  c.setCommentCount(i);
                  articlesCountDao.updateCount(c);
              }
              break;
          case INDUSTRY_APP:
              i = appCommentDao.queryCommentCount(comment.getContentId());
              c = appCountDao.queryCountById(comment.getContentId());
              if (c == null) {
                  c = new OptionCount(comment.getContentId(), 0, 0, 0, i, 0);
                  appCountDao.addCount(c);
              } else {
                  c.setCommentCount(i);
                  appCountDao.updateCount(c);
              }
              break;
          default:
              break;
        }
        return c;
    }

    public Comments queryCommentById(Integer id, FlyContentType type) {
        Comments cm = null;
        switch (type) {
          case QUESTION:
              break;
          case ANSWER:
              cm = answersCommentDao.queryCommentById(id);
              break;
          case ARTICLE:
              cm = articlesCommentDao.queryCommentById(id);
              break;
          case INDUSTRY_APP:
              cm = appCommentDao.queryCommentById(id);
              break;
          default:
              break;
        }
        return cm;
    }

    @Transactional
    public int editComment(Integer id, String content, Integer status, Date updateTime,
            FlyContentType type) throws Exception {
        Comments comment = new Comments();
        comment.setStatus(status);
        comment.setUpdateTime(updateTime);
        comment.setId(id);
        int i = 0;
        switch (type) {
            case ANSWER:
                i = answersCommentDao.editComment(comment);
                break;
            case ARTICLE:
                i = articlesCommentDao.editComment(comment);
                break;
            case INDUSTRY_APP:
                i = appCommentDao.editComment(comment);
                break;
            default:
                break;
        }
        return i;
    }
    
    @Transactional
    public int removeComment(Integer id, FlyContentType type) throws Exception {
        int i = 0;
        switch (type) {
            case ARTICLE:
                i = articlesCommentDao.removeCommentById(id);
                break;
            case INDUSTRY_APP:
                i = appCommentDao.removeCommentById(id);
                break;
            default:
                break;
        }
        return i;
    }

    public List<Comments> getCommentList(Long contentId, Long userId, Integer status,
            int pageIndex, int pageSize, FlyContentType type) throws Exception {
        String orderby = "create_time";
        String order = "desc";
        List<Comments> commentList = null;
        switch (type) {
          case ANSWER:
              commentList = answersCommentDao.getCommentList(contentId, userId, status, orderby,
                      order, pageIndex, pageSize);
              break;
          case ARTICLE:
              commentList = articlesCommentDao.getCommentList(contentId, userId, status, orderby,
                      order, pageIndex, pageSize);
              break;
          case INDUSTRY_APP:
              commentList = appCommentDao.getCommentList(contentId, userId, status, orderby,
                      order, pageIndex, pageSize);
              break;
          default:
              break;
        }
        return commentList;
    }

    public int getCommentCount(Long contentId, Long userId, Integer status, FlyContentType type) {
        int i = 0;
        switch (type) {
          case ANSWER:
              i = answersCommentDao.getCommentCount(contentId, userId, status);
              break;
          case ARTICLE:
              i = articlesCommentDao.getCommentCount(contentId, userId, status);
              break;
          case INDUSTRY_APP:
              i = appCommentDao.getCommentCount(contentId, userId, status);
              break;
          default:
              break;
        }
        return i;
    }

    /**
     * 根据appId 计算星星数
     * @param id appId
     */
    public BigDecimal getStarByAppId(Long id) {
        Float stars = appCommentDao.sumStarByCommentId(id);
        if (Objects.isNull(stars)) {
            return BigDecimal.ZERO;
        }
        int count = appCommentDao.getCommentCount(id, null, null);
        return BigDecimal.valueOf(stars).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
    }
}
